package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.bean.Cart;
import com.kgc.scd.bean.CartExample;
import com.kgc.scd.bean.CartItem;
import com.kgc.scd.service.BlCartService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created On : 2022/6/9.
 * <p>
 * Author     : 98730
 * <p>
 * Description: 购物车及下单操作入口
 */
@Slf4j
@RestController
public class BlCartController {

    @Autowired
    private BlCartService blCartService;

    /**
     * @author : 98730
     * @date   : 2022/6/10
     * @param  : []
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 添加购物车
     */
    @PostMapping("/addCart")
    public RequestResult<String> addCart(@RequestBody Cart cart){
        log.info("添加购物车：{}", cart);
        try {
            List<Cart> carts = blCartService.queryCartList(cart.getCartUid());
            log.info("查询该用户购物车列表，商品：{}", carts);
            for(Cart cart1 : carts){
                if (cart1.getCartProid().equals(cart.getCartProid())){
                    Cart cart2 = new Cart();
                    cart2.setCartId(cart1.getCartId());
                    cart2.setCartNum(cart.getCartNum() + cart1.getCartNum());
                    log.info("------ 如果商品相同，只增加数量 -------");
                    log.info("-----cart2:{} ------",cart2);
                    Integer count = blCartService.queryProStock(cart.getCartProid()).getProStock();
                    if (count < cart2.getCartNum()){
                        return ResultBuildUtil.fail("401",String.format("库存数不足，无法添加商品，当前商品库存数量为：%s",count));
                    }
                    blCartService.updateCartList(cart2);
                    return ResultBuildUtil.success("购物车已存在此商品，只增加数量");
                }
            }
            // 添加购物车
            blCartService.addCartByProId(cart);
            log.info("------ 添加购物车 -------");
            return ResultBuildUtil.success("商品添加购物车成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResultBuildUtil.fail("401",e.getMessage());
        }
    }

    /**
     * @author : 98730
     * @date   : 2022/6/10
     * @param  : [java.util.List<java.lang.Integer>]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 删除购物车
     */
    @PostMapping("/delCart")
    public RequestResult<String> delCart(@RequestParam List<Integer> cartId, @RequestBody UserAnno user){
        log.info("server-删除购物车，商品编号为：{}，用户为：{}", cartId, user);

        // 删除购物车
        if (blCartService.delCartById(cartId,user.getUserId())){
            return ResultBuildUtil.success("删除成功");
        }
        return ResultBuildUtil.fail("401","删除失败，请选择要删除的商品");
    }

    /**
     * @author : 98730
     * @date   : 2022/6/10
     * @param  : []
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Cart>>
     * @description : 显示购物车列表，每页3条数据
     */
    @PostMapping("/queryCart")
    public RequestResult<PageSupport<CartItem>> queryCartListByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                                                                    @RequestParam(defaultValue = "3") Integer pageSize,
                                                                    @RequestBody UserAnno user){
        return ResultBuildUtil.success(blCartService.queryCartListByPage(pageNo,pageSize,user.getUserId()));
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.lang.String, java.lang.Integer, com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 修改购物车
     */
    @PostMapping("/modCartList")
    public RequestResult<String> modCartList(@RequestParam String proId, @RequestParam(defaultValue = "1") Integer count, @RequestBody UserAnno userAnno){
        // 获取用户库存
        Integer stock = blCartService.queryProStock(proId).getProStock();
        // 根据用户id，获取购物车实体
        Cart cart = blCartService.queryCartListByUserId(proId,userAnno.getUserId());
        // 获取用户添加购物车的数量
        Integer proNum = cart.getCartNum();
        if ((count + proNum) > stock){
            return ResultBuildUtil.fail("401",String.format("库存不足，当前库存数量为：%s",stock));
        }
        if ((count + proNum) <= 0){
            return ResultBuildUtil.fail("401","商品数量不能小于1");
        }
        cart.setCartId(cart.getCartId());
        cart.setCartNum(count+proNum);
        return blCartService.updateCartList(cart) ? ResultBuildUtil.success("更新数量成功") : ResultBuildUtil.fail("401","操作非法");
    }
}
